package com.nineclock.document.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nineclock.common.entity.Result;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.common.utils.BeanHelper;
import com.nineclock.document.dto.DocFileHistoryDto;
import com.nineclock.document.mapper.DocFileHistoryMapper;
import com.nineclock.document.pojo.DocFileHistory;
import com.nineclock.document.service.DocFileHistoryServcie;
import com.nineclock.system.dto.SysCompanyUserDTO;
import com.nineclock.system.feign.SysUserFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class DocFileHistoryServcieImpl implements DocFileHistoryServcie {
    @Autowired
    private DocFileHistoryMapper docFileHistoryMapper;
    @Autowired
    private SysUserFeign sysUserFeign;
    @Override
    public List<DocFileHistoryDto> QueryHistory(Integer fileId) {
        //校验参数
        if (fileId == null) {
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }
        //组装参数
        LambdaQueryWrapper<DocFileHistory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DocFileHistory::getFileId, fileId);
        //进行查询
        List<DocFileHistory> docFileHistories = docFileHistoryMapper.selectList(wrapper);
        //获取员工id
        List<DocFileHistoryDto> docFileHistoryDtos = new ArrayList<>();
        for (DocFileHistory docFileHistory : docFileHistories) {
            Long userId = docFileHistory.getOperateUserId();
            //获取userName
            String userName = sysUserFeign.queryUserById(userId);

            //数据组装转换
            DocFileHistoryDto docFileHistoryDto = BeanHelper.copyProperties(docFileHistory, DocFileHistoryDto.class);
            docFileHistoryDto.setUserName(userName);
            docFileHistoryDtos.add(docFileHistoryDto);
        }

        return docFileHistoryDtos;
    }
}
